package com.exp.practice.workbench.controller.student;

import com.exp.practice.base.vo.PageVo;
import com.exp.practice.base.vo.ResultVo;
import com.exp.practice.workbench.domain.Question;
import com.exp.practice.workbench.domain.Student;
import com.exp.practice.workbench.domain.StudentTest;
import com.exp.practice.workbench.service.student.StudentExerciseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 巩固练习控制器
 * @author 13102
 * @date 2021/12/12 12:30
 * @version 1.0
 */
@Controller
@RequestMapping("/workbench/student/exercise")
public class StudentExerciseController {

    @Resource
    private StudentExerciseService studentExerciseService;
    /**
     * 开始练习
     * @param type
     * @param count
     * @param session
     * @return
     */
    @RequestMapping("/beginExercise.do")
    public ModelAndView beginExercise(String type, String count, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = studentExerciseService.getUndoQuestion(type, Integer.parseInt(count.substring(0, 1)), (Student)(session.getAttribute("user")));
        modelAndView.addObject("exerciseList",map.get("questionList"));
        session.setAttribute("exerciseList", map.get("questionList"));
        modelAndView.setViewName("workbench/student/consolidationExercises/doExercise");
        return modelAndView;
    }

    /**
     * 练习重做
     * @param qid
     * @param session
     * @return
     */
    @RequestMapping("/redo.do")
    public ModelAndView redo(String qid, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        List<Question> questions = studentExerciseService.getQuestion(qid);
        modelAndView.addObject("exerciseList", questions);
        session.setAttribute("exerciseList", questions);
        modelAndView.setViewName("workbench/student/consolidationExercises/doExercise");
        return modelAndView;
    }

    /**
     * 查询学生做错的题目
     * @param pageNo
     * @param pageSize
     * @param stuId
     * @return
     */
    @RequestMapping("/pageList.do")
    @ResponseBody
    public PageVo pageList(String pageNo, String pageSize, String stuId, boolean history){
        PageVo pageVo=new PageVo();
        //得到所查询数据
        List<StudentTest> list = null;
        int total = 0;
        if (history) {
            list = studentExerciseService.getWrongQuestion(pageNo, pageSize, stuId);
            total = studentExerciseService.getTotalWrong(stuId);
        } else {
            list = studentExerciseService.getHistoryQuestion(pageNo, pageSize, stuId);
            total = studentExerciseService.getTotalHistory(stuId);
        }
        pageVo.setTotal(total);
        pageVo.setDataList(list);
        return pageVo;
    }

    /**
     * 保存练习题
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/saveExercise.do")
    @ResponseBody
    public ResultVo saveExercise(HttpServletRequest request, HttpSession session) {
        ResultVo resultVo = new ResultVo();
        Student student = (Student) session.getAttribute("user");
        List<Question> list = (List<Question>) session.getAttribute("exerciseList");
        List<StudentTest> studentTests = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Question question = list.get(i);
            String stuAnswer=request.getParameter("answer_"+question.getQid());
            StudentTest studentTest = new StudentTest();
            studentTest.setStudent(student);
            studentTest.setQuestion(question);
            studentTest.setStuAnswer(stuAnswer);
            studentTests.add(studentTest);
        }
        int count = studentExerciseService.addExercise(studentTests);
        if (count != -1) {
            resultVo.setSuccess(true);
            resultVo.setMsg("保存成功" + count);
        } else {
            resultVo.setSuccess(false);
            resultVo.setMsg("保存失败");

        }
        return resultVo;
    }
}
